package edu.nwpu.handler;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Component
@Aspect
public class ServiceExceptionHandler {

    @Pointcut("@annotation(edu.nwpu.annotation.MyServiceAnnotation) || @within(edu.nwpu.annotation.MyServiceAnnotation)")
    public void getPointCut(){}

    @Pointcut("execution(public * edu.nwpu.service.*.*(..))")
    public void cut(){}

    @Around("getPointCut()")
    public Object exceptionHandle(ProceedingJoinPoint point){
        System.out.println("事务开启======================");
        Object proceed = null;
        try {
            // proceed = point.getSignature().getDeclaringType().getConstructor().newInstance();
            proceed = point.proceed();
            System.out.println("事务成功======================");
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("ServiceExceptionHandler发生错误=========");
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return proceed;
        }
        
    }
}
